﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using MQTTServer.Model;
using MQTTServer.Model.UDPEvents;

namespace MQTTServer.Bll
{
    public class UPDServerBase
    {
        /// <summary>
        /// UDP服务端IP
        /// </summary>
        public string ServerIp = "127.0.0.1";
        /// <summary>
        /// UDP服务器端口
        /// </summary>
        public int ServerPort = 4600;
        /// <summary>
        /// 服务端UDP对象
        /// </summary>
         protected UdpClient updServer;
       // protected Socket updServer_Socket;
        /// <summary>
        /// 客户端UDP对象
        /// </summary>
        protected UdpClient updClient = new UdpClient();
        /// <summary>
        /// 远程客户端IP地址信息
        /// </summary>
        protected IPEndPoint ClientIpPoint;
        protected IPEndPoint ServerIpPoint;
        /// <summary>
        /// 客户端集合
        /// </summary>
        public ClientListModel Server_ClientListModel = new ClientListModel();
        /// <summary>
        /// 服务器连接事件
        /// </summary>
        public event EventHandler<ServerConnectdEventArgs> ServerConnectdEvent;
        /// <summary>
        /// 服务器发送数据事件
        /// </summary>
        public event EventHandler<ClientDataEventArgs> SendDatadEvent;
        /// <summary>
        /// 服务器接收事件
        /// </summary>
        public event EventHandler<ClientDataEventArgs> ReceivedDataEvent;
        /// <summary>
        /// 服务器接收事件
        /// </summary>
       // public event EventHandler<ClientMessageDataEventArgs> ReceivedMessageDataEvent;
        /// <summary>
        /// 新客户连接事件
        /// </summary>
        public event EventHandler<ClientMessageDataEventArgs> NewClientConnectdEvent;
        /// <summary>
        /// 客户超时掉线关闭事件
        /// </summary>
        public event EventHandler<ClientMessageDataEventArgs> CloseClientConnectdEvent;
        /// <summary>
        /// 产生错误事件
        /// </summary>
        public event EventHandler<ExceptionEventArgs> ExceptionEvent;

        
        /// <summary>
        /// 触发服务器连接事件的方法
        /// </summary>
        /// <param name="err"></param>
        protected virtual void OnServerConnectd(IPEndPoint Point, string message)
        {
            EventHandler<ServerConnectdEventArgs> handler = this.ServerConnectdEvent;
            if (handler != null)
            {
                ServerConnectdEventArgs e = new ServerConnectdEventArgs(Point, message);
                handler(this, e);
            }
        }
        /// <summary>
        /// 发送数据事件
        /// </summary>
        /// <param name="clientData"></param>
        protected virtual void OnServerSendDatad(string clientData)
        {
            EventHandler<ClientDataEventArgs> handler = this.SendDatadEvent;
            if (handler != null)
            {
                ClientDataEventArgs e = new ClientDataEventArgs(clientData);
                handler(this, e);
            }

        }
        /// <summary>
        /// 接收数据事件
        /// </summary>
        /// <param name="clientData"></param>
        protected virtual void OnServerReceived(string clientData)
        {
            EventHandler<ClientDataEventArgs> handler = this.ReceivedDataEvent;
            if (handler != null)
            {
                ClientDataEventArgs e = new ClientDataEventArgs(clientData);
                handler(this, e);
            }
        }
        /// <summary>
        /// 接收或发送字符串数据事件
        /// </summary>
        //protected virtual void OnReceived_SendMessageDatad(string mesgData)
        //{
        //    EventHandler<ClientMessageDataEventArgs> handler = this.ReceivedMessageDataEvent;
        //    if (handler != null)
        //    {
        //        ClientMessageDataEventArgs e = new ClientMessageDataEventArgs(mesgData);
        //        handler(this, e);
        //    }
        //}
        protected virtual void OnNewClientConnectd(string mesgData)
        {
            EventHandler<ClientMessageDataEventArgs> handler = this.NewClientConnectdEvent;
            if (handler != null)
            {
                ClientMessageDataEventArgs e = new ClientMessageDataEventArgs(mesgData);
                handler(this, e);
            }
        }
        protected virtual void OnNewClientConnectd(ClientPayloadDataModel client)
        {
            EventHandler<ClientMessageDataEventArgs> handler = this.NewClientConnectdEvent;
            if (handler != null)
            {
                ClientMessageDataEventArgs e = new ClientMessageDataEventArgs(client);
                handler(this, e);
            }
        }
        protected virtual void OnCloseClientConnectd(ClientPayloadDataModel client)
        {
            EventHandler<ClientMessageDataEventArgs> handler = this.CloseClientConnectdEvent;
            if (handler != null)
            {
                ClientMessageDataEventArgs e = new ClientMessageDataEventArgs(client);
                handler(this, e);
            }
        }
        
        /// <summary>
        /// 产生错误事件
        /// </summary>
        /// <param name="ex"></param>
        protected virtual void OnServerExceptiond(string str, Exception ex)
        {
            EventHandler<ExceptionEventArgs> handler = this.ExceptionEvent;
            if (handler != null)
            {
                ExceptionEventArgs e = new ExceptionEventArgs(ex);
                handler(this, e);
            }
        }
        
    }
}
